import { getData } from '@vimi/utils-api'
import { SERVICE_NAME } from '@/gov/plugins/constant'

const state = {
  enumCategory: [],
}

const getters = {
  enumCategory: state => state.enumCategory,
}

const actions = {
  async getEnumCategory({ commit }, force = false) {
    if (!force && state.enumCategory.length) {
      return Promise.resolve(state.enumCategory)
    }

    const res = await getData(SERVICE_NAME, 'credit_enum_category')
    const data = (res && res.data) || []
    commit('enumCategory', data)
    return data
  },
}

const mutations = {
  enumCategory: (state, payload) => (state.enumCategory = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
